package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetDetailQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetListQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetListQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetManageApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetReq;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/13
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class AssetManageApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final AssetManageApiService INSTANCE = new AssetManageApiService();
    }

    public static AssetManageApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private AssetManageApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private AssetManageApiGrpc.AssetManageApiBlockingStub getStub(ManagedChannel channel) {
        return AssetManageApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }


    /**
     * 资产列表查询——默认不查库存中的资产
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AssetListQueryResp> queryAssetList(
            String assetTypeId,
            CallBack<AssetListQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AssetListQueryResp>(callBack) {
            @Override
            protected AssetListQueryResp doRequestData(ManagedChannel channel) {
                AssetListQueryReq message = AssetListQueryReq.newBuilder()
                        .setComQuery(ComQuery.newBuilder()
                                .setPage(1)
                                .setPageSize(10000).build())
                        .setTenantNo(1)
                        .setAssetTypeId(assetTypeId)
                        .setQueryTypeTag(1)// 是否只查询本级 1只查询本分类下设备 不传默认查所有
                        .build();
                Logger.d(message);
                AssetListQueryResp response = null;
                try {
                    response = getStub(channel).queryAssetList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAssetList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 资产列表查询——默认不查库存中的资产
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AssetListQueryResp> queryAssetListByKeyword(
            String keyword,
            CallBack<AssetListQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AssetListQueryResp>(callBack) {
            @Override
            protected AssetListQueryResp doRequestData(ManagedChannel channel) {
                AssetListQueryReq message = AssetListQueryReq.newBuilder()
                        .setComQuery(ComQuery.newBuilder()
                                .setPage(1)
                                .setPageSize(10000).build())
                        .setKeyword(keyword)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AssetListQueryResp response = null;
                try {
                    response = getStub(channel).queryAssetList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAssetList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 资产详情查询
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AssetDetailQueryResp> queryAssetDetail(
            String assetsId,
            String userId,
            CallBack<AssetDetailQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AssetDetailQueryResp>(callBack) {
            @Override
            protected AssetDetailQueryResp doRequestData(ManagedChannel channel) {
                AssetReq message = AssetReq.newBuilder()
                        .addIds(assetsId)
                        .setTenantNo(1)
                        .setUserId(userId)
                        .build();
                Logger.d(message);
                AssetDetailQueryResp response = null;
                try {
                    response = getStub(channel).queryAssetDetail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAssetDetail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
